<template>
  <div class="form-page">
    <div class="block-wrap form-container">
      <el-form ref="form" :model="form" :rules="rules" label-width="8em">
        <el-form-item label="用户名">
          <el-input v-model.trim="userName" :disabled="true" />
        </el-form-item>
        <el-form-item label="旧密码" prop="password">
          <el-input
            v-model.trim="form.password"
            placeholder="请输入旧密码"
            :type="passwordType"
          />
          <span
            class="show-pwd"
            @click="
              passwordType = passwordType === 'password' ? '' : 'password'
            "
          >
            <svg-icon
              :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
            />
          </span>
        </el-form-item>
        <el-form-item label="新密码" prop="newpassword1">
          <el-input
            v-model.trim="form.newpassword1"
            placeholder="长度为6~25个字符，只能包含字母、数字、符号"
            :type="passwordType"
          />
          <span
            class="show-pwd"
            @click="
              passwordType = passwordType === 'password' ? '' : 'password'
            "
          >
            <svg-icon
              :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
            />
          </span>
        </el-form-item>
        <el-form-item label="确认新密码" prop="againPassword">
          <el-input
            v-model.trim="form.againPassword"
            placeholder="再次输入新密码"
            :type="passwordReType"
          />
          <span
            class="show-pwd"
            @click="
              passwordReType = passwordReType === 'password' ? '' : 'password'
            "
          >
            <svg-icon
              :icon-class="passwordReType === 'password' ? 'eye' : 'eye-open'"
            />
          </span>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :loading="loading" @click="submit"
            >提交</el-button
          >
          <el-button @click="cancelModify">取消</el-button>
        </el-form-item>
      </el-form>
      <div class="subImg">
        <img src="@/assets/subImg.png" alt="" />
      </div>
    </div>
    <el-dialog
      :visible.sync="dialogTableVisible"
      width="560px"
      :show-close="false"
      top="25vh"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <el-result icon="success" title="密码修改成功，请重新登录">
        <template slot="extra">
          <el-button type="primary" size="medium" @click="goto">确定</el-button>
        </template>
      </el-result>
    </el-dialog>
  </div>
</template>

<script>
import { editInfo } from "@/api/user";
export default {
  name: "AddMenu",
  data() {
    const validatenewpassword1 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入确认新密码"));
      } else if (value !== this.form.newpassword1) {
        callback(new Error("确认密码与新密码不一致，请重新输入。"));
      } else {
        callback();
      }
    };
    return {
      passwordType: "password",
      passwordReType: "password",
      form: {
        username: "",
        password: "", //旧密码
        newpassword1: "",
        againPassword: "" //再次确认的密码
      },
      rules: {
        password: [
          { required: true, message: "请输入旧密码", trigger: "blur" }
        ],
        newpassword1: [
          { required: true, message: "请输入新密码", trigger: "blur" },
          { min: 6, max: 25, message: "长度在 6 到 25 个字符", trigger: "blur" }
        ],
        againPassword: [
          { required: true, message: "请再次输入新密码", trigger: "blur" },
          { trigger: "blur", validator: validatenewpassword1 }
        ]
      },
      loading: false,
      dialogTableVisible: false
    };
  },
  computed: {
    userName() {
      return this.$store.state.user.name;
    }
  },
  created() {},
  methods: {
    submit() {
      this.form.username = this.userName;
      this.loading = true;
      this.$refs.form.validate(valid => {
        if (valid) {
          editInfo(this.form)
            .then(res => {
              if (res.code === 200) {
                this.loading = false;
                this.dialogTableVisible = true;
              }
            })
            .catch(err => {
              this.loading = false;
            });
        } else {
          console.log("error submit!!");
          this.loading = false;
          return false;
        }
      });
    },
    async goto() {
      await this.$store.dispatch("user/logout");
      this.$router.push(`/login?redirect=${this.$route.fullPath}`);
    },
    cancelModify() {
      this.$router.go(-1);
    }
  }
};
</script>

<style lang="scss">
@import "~@/styles/page/form-page.scss";
</style>
<style lang="scss" scoped>
.form-page .form-container {
  padding: 80px 160px 80px 160px;
  background-color: #ffffff;
  .title {
    text-indent: 70px;
  }
  .el-form {
    float: left;
    width: 760px;
  }
  .subImg {
    float: left;
    margin-left: 200px;
  }
  .el-input.is-disabled .el-input__inner {
    background-color: #f5f5f5;
    color: #000000;
  }
}
.form-page {
  .el-dialog {
    .el-dialog__header {
      display: none;
    }
    .el-dialog__body {
      padding: 0;
    }
  }
  .el-button.el-button--primary.el-button--medium {
    background-color: #1890ff;
    border-color: #1890ff;
  }
}
</style>
